<?php 

namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Base;
use app\admin\model\system;
use app\admin\model\navi;
use think\Request;
class Systemset extends Base{
	public function index(){
		$res = db('system')->where('id', '=', 1)->find();
		return $this->fetch('admin/systemset',['res'=>$res]);
	}
	//判断是否是POST请求
	public function ispost(){
		if (!Request::instance()->isPost()){
			$this->redirect('index');
		}
	}
	public function systemsetpost(){
		$this->ispost();
		//此处写入Ajax返回数据
		//修改网站信息
		$systemset = system::get(1);
		$systemset->webname		= 	input('webname');
  		$systemset->keyword		=	input('keyword');
  		$systemset->copyright	=	input('copyright');
  		$systemset->kor 		=	input('kor');
  		$systemset->describe	=	input('describe');
  		$systemset->bottom		=	input('bottom');
  		$res = $systemset->save();
		if($res > 0){
			$data = ["result"=>1, "msg"=>"编辑成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"编辑失败(未做更改)"];
			echo json_encode($data);
		}
	}
	public function nav(){
		$res = db('navi')->order('num', 'dec')->select();
		return $this->fetch('admin/nav',['res'=>$res]);
	}
	public function navpostadd(){
		$this->ispost();
		$navi = new navi;
		$navi->num 		=	input('num');
		$navi->navname	=	input('name');
		$navi->url 		=	input('url');
		$navi->issystem =	'n';
		$res = $navi->save();	
		if($res > 0){
			$data = ["result"=>1, "msg"=>"添加成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"添加失败"];
			echo json_encode($data);
		}
	}
	public function navpostdel(){
		$this->isPost();
		$id = input('id');
		$nav = navi::get($id);
		if($nav->delete()){
			$data = ["result"=>1, "msg"=>"删除成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"删除失败"];
			echo json_encode($data);
		}
	}
	public function navpostedit(){
		$this->isPost();
		$id = input('id');
		$nav = navi::get($id);
		$nav->num 		=	input('newnum');
		$nav->navname	=	input('newname');
		$nav->url 		=	input('newurl');
		$nav->issystem =	'n';
		if ($nav->save()) {
			$data = ["result"=>1, "msg"=>"编辑成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"编辑失败"];
			echo json_encode($data);
		}
	}
}


 ?>